//
//  goveSearchVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/6/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "goveSearchVC.h"
#import "mySearchBar.h"
#import "goveForCell.h"
#import "goveDetailVC.h"
@interface goveSearchVC ()<UITextFieldDelegate>{
    NSArray * dataArr;
    NSMutableDictionary * dictData;
}
@property (strong, nonatomic) mySearchBar * search;

@end

@implementation goveSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataArr = [NSArray array];
    dictData = [NSMutableDictionary dictionary];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.search = [mySearchBar searchBar];
    
    _search.delegate = self;
    
    self.navigationItem.titleView = _search;
    
    
    [self.search becomeFirstResponder];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:dataArr.count];
    return dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 注册
    UINib * nib = [UINib nibWithNibName:@"goveForCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"detailCell"];
    goveForCell *detailCell = [tableView dequeueReusableCellWithIdentifier:@"detailCell" forIndexPath:indexPath];
    
    NSDictionary * dict = dataArr[indexPath.row];
    
    detailCell.headImg.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    detailCell.headImg.layer.cornerRadius = 4.f;
    detailCell.headImg.image = [UIImage imageNamed:@"20100403114758394_4147"];
    detailCell.name.text = dict[@"userName"];
    detailCell.detail.text = dict[@"address"];
    // Configure the cell...
    
    return detailCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    goveDetailVC * govedetailVC = [[goveDetailVC alloc]init];
    NSDictionary * dict = dataArr[indexPath.row];
    govedetailVC.addressStr = dict[@"userName"];
    govedetailVC.userType = @"3";
    govedetailVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:govedetailVC animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(void)sendRequest{
    
    [self showRoundProgressWithTitle:@"请稍后..."];
    // 请求数据
    
    NSString *str = @"getGovePaixu_app_myHome.im";
    
    [LSHttpTool sendHttpData:str params:dictData success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            dataArr = dicInfo[@"data"];
            
            if (dataArr.count < 1) {

                [self showSingleIconBubble:@"请输入正确的关键词"];
                
            }
            
            [self.tableView reloadData];
            [self hideBubble];
        }else{

            [self showSingleIconBubble:@"请输入正确的关键词"];
        }
        
    } failure:^(NSError *error) {

       [self showSingleIconBubble:@"网络连接错误"];
        
    }];

}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.text.length >= 1) {
        [dictData setValue:textField.text forKey:@"userName"];
        [self sendRequest];
        [self.view endEditing:YES];
        self.search.text = nil;
        [self.search resignFirstResponder];
        
  
        
    }else{


    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length >= 1) {
        [dictData setValue:textField.text forKey:@"userName"];
        [self sendRequest];
        [self.view endEditing:YES];
        self.search.text = nil;
        [self.search resignFirstResponder];

        return YES;
        
    }else{

        [self showSingleIconBubble:@"关键词不能为空"];
        return NO;
    }
    
}

@end
